<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="ZhangPing">





<title>andfix浅析与使用 | ZhangPing&#39;s Blog</title>



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 4.2.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">ZhangPing&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">ZhangPing&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">andfix浅析与使用</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">ZhangPing</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">April 21, 2016&nbsp;&nbsp;14:00:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Android-%E7%BB%84%E4%BB%B6/">Android-组件</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <h3 id="一、AndFix原理"><a href="#一、AndFix原理" class="headerlink" title="一、AndFix原理"></a>一、AndFix原理</h3><h5 id="1-AndFix的原理就是方法的替换，把有bug的方法替换成补丁文件中的方法。"><a href="#1-AndFix的原理就是方法的替换，把有bug的方法替换成补丁文件中的方法。" class="headerlink" title="1.AndFix的原理就是方法的替换，把有bug的方法替换成补丁文件中的方法。"></a>1.AndFix的原理就是方法的替换，把有bug的方法替换成补丁文件中的方法。</h5><p><a href="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix浅析和使用/1.png" target="_blank" rel="noopener"><img src="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix%E6%B5%85%E6%9E%90%E5%92%8C%E4%BD%BF%E7%94%A8/1.png" alt="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix浅析和使用/1.png"></a></p>
<p><strong><em>注：在Native层使用指针替换的方式替换bug方法，已达到修复bug的目的。</em></strong></p>
<h5 id="2-使用AndFix修复热修复的整体流程："><a href="#2-使用AndFix修复热修复的整体流程：" class="headerlink" title="2.使用AndFix修复热修复的整体流程："></a>2.使用AndFix修复热修复的整体流程：</h5><p><a href="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix浅析和使用/1.png" target="_blank" rel="noopener"><img src="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix%E6%B5%85%E6%9E%90%E5%92%8C%E4%BD%BF%E7%94%A8/2.png" alt="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix浅析和使用/1.png"></a></p>
<h5 id="3-方法替换过程："><a href="#3-方法替换过程：" class="headerlink" title="3.方法替换过程："></a>3.方法替换过程：</h5><p><a href="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix浅析和使用/1.png" target="_blank" rel="noopener"><img src="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix%E6%B5%85%E6%9E%90%E5%92%8C%E4%BD%BF%E7%94%A8/3.png" alt="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix浅析和使用/1.png"></a></p>
<h3 id="二、patch文件的生成"><a href="#二、patch文件的生成" class="headerlink" title="二、patch文件的生成"></a>二、patch文件的生成</h3><p>使用工具：apkpatch-1.0.3 </p>
<p><a href="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix浅析和使用/1.png" target="_blank" rel="noopener"><img src="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix%E6%B5%85%E6%9E%90%E5%92%8C%E4%BD%BF%E7%94%A8/4.png" alt="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix浅析和使用/1.png"></a></p>
<p>原理：根据两个apk包，生成一个差异文件，就是所谓的补丁文件即patch文件。</p>
<p>命令 : apkpatch.bat -f new.apk -t old.apk -o output1 -k debug.keystore -p 123456 -a andfix -e 123456</p>
<p>-f &lt;new.apk&gt; ：新版本</p>
<p>-t &lt;old.apk&gt; : 旧版本</p>
<p>-o <output> ： 输出目录</p>
<p>-k <keystore>： 打包所用的keystore</p>
<p>-p <password>： keystore的密码</p>
<p>-a <alias>： keystore 用户别名</p>
<p>-e <alias password>： keystore 用户别名密码</p>
<h3 id="三、Android上如何使用"><a href="#三、Android上如何使用" class="headerlink" title="三、Android上如何使用"></a>三、Android上如何使用</h3><h5 id="1-Application中初始化以及补丁加载"><a href="#1-Application中初始化以及补丁加载" class="headerlink" title="1.Application中初始化以及补丁加载"></a>1.Application中初始化以及补丁加载</h5><h6 id="1-在自定义Application中初始化，为了更早的修复应用中的bug。"><a href="#1-在自定义Application中初始化，为了更早的修复应用中的bug。" class="headerlink" title="(1).在自定义Application中初始化，为了更早的修复应用中的bug。"></a>(1).在自定义Application中初始化，为了更早的修复应用中的bug。</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.alipay.euler.andfix.patch.PatchManager;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainApplication</span> <span class="keyword">extends</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> PatchManager mPatchManager;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">super</span>.onCreate();</span><br><span class="line">        <span class="comment">// 初始化patch管理类</span></span><br><span class="line">        mPatchManager = <span class="keyword">new</span> PatchManager(<span class="keyword">this</span>);</span><br><span class="line">        <span class="comment">// 初始化patch版本</span></span><br><span class="line">        mPatchManager.init(<span class="string">"1.0"</span>);</span><br><span class="line">        <span class="comment">// 加载已经添加到PatchManager中的patch</span></span><br><span class="line">        mPatchManager.loadPatch();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="2-如果有新的补丁需要修复，下载完成后，进行以下操作"><a href="#2-如果有新的补丁需要修复，下载完成后，进行以下操作" class="headerlink" title="(2).如果有新的补丁需要修复，下载完成后，进行以下操作"></a>(2).如果有新的补丁需要修复，下载完成后，进行以下操作</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//添加patch，只需指定patch的路径即可，补丁会立即生效</span></span><br><span class="line">mPatchManager.addPatch(path);</span><br></pre></td></tr></table></figure>

<h6 id="3-当apk版本升级，需要把之前patch文件的删除，需要以下操作"><a href="#3-当apk版本升级，需要把之前patch文件的删除，需要以下操作" class="headerlink" title="(3).当apk版本升级，需要把之前patch文件的删除，需要以下操作"></a>(3).当apk版本升级，需要把之前patch文件的删除，需要以下操作</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//删除所有已加载的patch文件</span></span><br><span class="line">mPatchManager.removeAllPatch();</span><br></pre></td></tr></table></figure>

<h6 id="4-APK热修复打补丁流程"><a href="#4-APK热修复打补丁流程" class="headerlink" title="(4).APK热修复打补丁流程"></a>(4).APK热修复打补丁流程</h6><p><a href="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix浅析和使用/1.png" target="_blank" rel="noopener"><img src="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix%E6%B5%85%E6%9E%90%E5%92%8C%E4%BD%BF%E7%94%A8/5.png" alt="https://gitee.com/zhangpingV5/BlogPic/raw/master/andfix浅析和使用/1.png"></a></p>
<h5 id="2-在Android-Studio使用"><a href="#2-在Android-Studio使用" class="headerlink" title="2.在Android Studio使用"></a>2.在Android Studio使用</h5><p>maven dependency:</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.alipay.euler&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;andfix&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;0.3.1&lt;/version&gt;</span><br><span class="line">    &lt;type&gt;aar&lt;/type&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<p>or gradle dependency:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">dependencies &#123;</span><br><span class="line">    compile <span class="string">'com.alipay.euler:andfix:0.3.1@aar'</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="3-在Eclipse使用"><a href="#3-在Eclipse使用" class="headerlink" title="3.在Eclipse使用"></a>3.在Eclipse使用</h5><p>把Java层的代码引入到你的工程，配置Ndk开发环境并把jni Native代码添加进来。</p>
<h5 id="4-代码混淆-ProGuard"><a href="#4-代码混淆-ProGuard" class="headerlink" title="4.代码混淆(ProGuard)"></a>4.代码混淆(ProGuard)</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">-keep <span class="class"><span class="keyword">class</span> * <span class="keyword">extends</span> <span class="title">java</span>.<span class="title">lang</span>.<span class="title">annotation</span>.<span class="title">Annotation</span></span></span><br><span class="line"><span class="class">-<span class="title">keepclasseswithmembernames</span> <span class="title">class</span> * </span>&#123;</span><br><span class="line">    <span class="keyword">native</span> &lt;methods&gt;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意：每次产生的apatch文件用的名字不能相同的，相同会导致只有第一次的补丁能生效。</p>

        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>ZhangPing</span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://zhangpingv5.gitee.io/2016/04/21/andfix%E6%B5%85%E6%9E%90%E4%B8%8E%E4%BD%BF%E7%94%A8/">https://zhangpingv5.gitee.io/2016/04/21/andfix%E6%B5%85%E6%9E%90%E4%B8%8E%E4%BD%BF%E7%94%A8/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>转载请注明出处</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>Self-descipline grants me freedom.</span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/%E7%83%AD%E4%BF%AE%E5%A4%8D/"># 热修复</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2016/06/20/Android%20Studio%E4%B8%8Alint%E7%9A%84%E4%BD%BF%E7%94%A8/">Android Studio上lint的使用</a>
            
            
            <a class="next" rel="next" href="/2016/04/09/360%E5%8A%A0%E5%9B%BA%E5%8A%A9%E6%89%8B%E5%8A%A0%E5%9B%BA%E6%B5%81%E7%A8%8B/">360加固助手加固流程</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© ZhangPing | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
